package com.w7vm.menuweb.dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import com.w7vm.menuweb.entity.CardapioBuffetWeb;
import com.w7vm.menuweb.entity.CardapioInfantilWeb;
import com.w7vm.menuweb.entity.RestauranteFilialWeb;
import com.w7vm.menuweb.entity.RestauranteSedeWeb;

@Stateless
public class CardapioInfantilDAO extends GenericDAO<CardapioInfantilWeb> {

	
	@PersistenceContext
	private EntityManager em;
	
	public CardapioInfantilDAO() {
		super(CardapioInfantilWeb.class);
	}


	public int saveCardapioInfantil(CardapioInfantilWeb infantilEntity) {
		System.out.println("[IAM-EJB][saveCardapioInfantil] :");
		em.persist(infantilEntity);
		em.flush();
		return infantilEntity.getIdCardapioInfantilWeb();
	}

	public CardapioInfantilWeb mergeUpdate(CardapioInfantilWeb infantilEntity) {
		System.out.println("[IAM-EJB][mergeUpdate] :"+ infantilEntity.getIdCardapioInfantilWeb());

		return super.update(infantilEntity);
	}

	public void deleteCardapioInfantil(int id) {
		System.out.println("[IAM-EJB][deleteCardapioInfantil] :"+ id);
		CardapioInfantilWeb infantilEntity = new CardapioInfantilWeb(); 
		infantilEntity = em.find(CardapioInfantilWeb.class, id);
		em.remove(infantilEntity);
		em.flush();
	}

	public List<CardapioInfantilWeb> findCardapioInfantilByIDRestauranteSede(int id) {
		System.out.println("[IAM-EJB][findCardapioInfantilByIDRestauranteSede] :"+ id);
		Map<String, Object> parameters = new HashMap<String, Object>();
		parameters.put("idRestaurante", id);
		try{
			return super.findByCriterion(CardapioInfantilWeb.FIND_INFANTIL_BY_IDRESTAURANTESEDE, parameters);
		}catch (NoResultException e){
			System.err.println(e.getMessage());
			return null;
		}
	}

	public List<CardapioInfantilWeb> findCardapioInfantilByIDRestauranteFilial(int id) {

		System.out.println("[IAM-EJB][findCardapioInfantilByIDRestauranteFilial] :"+ id);
		
		Map<String, Object> parameters = new HashMap<String, Object>();
		parameters.put("idRestaurante", id);
		try{
			return super.findByCriterion(CardapioInfantilWeb.FIND_INFANTIL_BY_IDRESTAURANTEFILIAL, parameters);
		}catch (NoResultException e){
			System.err.println(e.getMessage());
			return null;
		}
	}
	
	
	
	public CardapioInfantilWeb findCardapioInfantilByID(int entityID) {
		System.out.println("[CARDAPIO-G2][findCardapioInfantilByID] :"+ entityID);
		try{
			Map<String, Object> parameters = new HashMap<String, Object>();
			parameters.put("idCardapioInfantil", entityID);   
			return super.findOneResult(CardapioInfantilWeb.FIND_INFANTIL_BY_ID, parameters);
		}catch (NoResultException e){
			System.err.println(e.getMessage());
			return null;
		}
	}
	
	
	public int alterarStatusCardapioInfantil(CardapioInfantilWeb infantilEntity) {
		System.out.println("[IAM-EJB][alterarStatusCardapioInfantil] :"+ infantilEntity.getIdCardapioInfantilWeb());

		try{
			Query q1 = em.createQuery("UPDATE CardapioInfantilWeb SET diasSemana = ?, dtInicio = ?, dtTermino = ?," +
					" status = ? WHERE idCardapioInfantilWeb = ?");
	
			q1.setParameter(1, infantilEntity.getDiasSemana());
			q1.setParameter(2, infantilEntity.getDtInicio());
			q1.setParameter(3, infantilEntity.getDtTermino());
			q1.setParameter(4, infantilEntity.getStatus());
			q1.setParameter(5, infantilEntity.getIdCardapioInfantilWeb());
			
			int updated = q1.executeUpdate();
			return updated;

		}catch (NoResultException e){
			System.err.println(e.getMessage());
			return 0;
		}
	}


	
}
